package com.hfh.homework;

import org.junit.Test;

import java.io.*;

/**
 * @Author hfh
 * @Date 2022 09 07 14 19
 **/
public class Work3 {
    public static void main(String[] args) {
        File src = new File("D:\\JavaProject\\newlearn\\day22\\file\\txt");
        File target = new File("D:\\JavaProject\\newlearn\\day22\\file\\jpg");
        copy(src,target);
    }

    public  static void copy(File src,File target){
        File[] arr = src.listFiles();
        for (File f: arr){
            if(f.isDirectory()){
                File newtarget = new File(target.getPath()+"\\"+f.getName());
                newtarget.mkdir();
                copy(f,newtarget);
            }else {
                try(BufferedInputStream bis = new BufferedInputStream(new FileInputStream(f));
                    BufferedOutputStream bos = new BufferedOutputStream(
                            new FileOutputStream(new File(target.getPath()+"\\"+f.getName())))){
                    byte[] bytes = new byte[1024];
                    int len;
                    while ((len=bis.read(bytes))!=-1){
                        bos.write(bytes,0,len);
                        bos.flush();
                    }
                }catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}
